package me.numark;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.baidu.android.pushservice.BasicPushNotificationBuilder;
import com.baidu.android.pushservice.PushManager;
import com.baidu.android.pushservice.PushMessageReceiver;
import com.google.gson.Gson;

import java.util.List;

import me.numark.model.PushMessage;
import me.numark.model.User;
import me.numark.receiver.MessageBroadCastReceiver;
import me.numark.utils.Global;
import me.numark.utils.Settings;

/**
 * Created by Evan on 14/11/19.
 */
public class PushReceiver extends PushMessageReceiver {

    private static final String LOG_TAG = "NumaPush";
    Gson mGson = new Gson();
    NotificationManager notificationManager;
    @Override
    public void onBind(final Context context, final int errorCode, String appid,
                       String userId, String channelId, String requestId) {
        if (errorCode == 0) {
            Global.BAIDU_USER_ID = userId;
            Global.BAIDU_CHANNEL_ID = channelId;
            Log.d(LOG_TAG,""+userId + "  " +channelId);
            Global.bindPush();
        }
    }

    @Override
    public void onMessage(final Context context, String message, String customContentString) {
        Log.e(LOG_TAG, "**********Get push:" + message);
//        MessageUtils.asyncUpdateMsg(context);
        if (message == null) {
            return;
        }
        PushMessage msg = mGson.fromJson(message, PushMessage.class);
        if (msg == null || (msg.custom_content != null && msg.custom_content.hide > 0)) {
            return;
        }
        int uid = 0;
        if (msg.custom_content != null) {
            uid = msg.custom_content.uid;
        }
        if (uid == Global.getCurrentUid()){
            return;
        }
        //Log.d(LOG_TAG,""+(uid == Global.currentChatUid));
        if (uid == Global.currentChatUid) {
            return;
        }
        MessageBroadCastReceiver.sendMessageBroadcast(context,true,msg);




        /*Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.notification)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher))
                .setContentTitle(msg.title)
                .setContentText(msg.description)
                .setContentIntent(pendingIntent);
        if (notificationManager == null) {
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Notification build = nb.build();
        build.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(uid, build);*/





       // Intent intent = new Intent(context, MainActivity.class);
      /*  NotificationCompat.Builder builder
                = new NotificationCompat.Builder(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 998, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Resources resources = context.getResources();
        Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher);
        builder
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(msg.title)
                .setContentText(msg.description)
                .setContentIntent(pendingIntent)
                .setLargeIcon(bitmap);
        Notification notification = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
        managerCompat.notify(998, notification);*/

       /*BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder();
        builder.setNotificationTitle(msg.title);
        builder.setNotificationText(msg.description);
        builder.setStatusbarIcon(R.drawable.ic_launcher);
        builder.setNotificationFlags(Notification.DEFAULT_ALL);
        PushManager.setDefaultNotificationBuilder(context,builder);*/
    }

    @Override
    public void onNotificationClicked(Context context, String title, String description, String customContentString) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.getApplicationContext().startActivity(intent);
    }

    @Override
    public void onNotificationArrived(Context context, String s, String s1, String s2) {

    }


    @Override
    public void onSetTags(Context context, int errorCode, List<String>
            successTags, List<String> failTags, String requestId) {
    }

    @Override
    public void onDelTags(Context context, int errorCode, List<String> successTags, List<String> failTags, String requestId) {
    }

    @Override
    public void onListTags(Context context, int errorCode, List<String> tags, String requestId) {
    }

    @Override
    public void onUnbind(Context context, int errorCode, String requestId) {
    }

}
